package cn.stronglink.asset.module.assetmanage.transfer.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.model.Transfer;
import cn.stronglink.asset.model.TransferDetail;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.assetmanage.transfer.service.TransferService;

import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.util.WebUtil;

@Controller
@RequestMapping(value="transfer")
public class TransferController extends AbstractController{
	@Autowired
	private TransferService transferService;
	
	/**
	 * 插入迁移单
	 */
	@OperateLog(module = "迁移单管理",desc="添加迁移单", type = OpType.ADD)
	@RequestMapping(value="create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Transfer transfer) {
		transferService.create(transfer);
		return setSuccessModelMap(modelMap,null);
	}
	/**
	 * 移除
	 */
	@OperateLog(module = "迁移单管理",desc="删除迁移单", type = OpType.DEL)
	@RequestMapping(value="remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id){
		transferService.remove(id);
		return setSuccessModelMap(modelMap,null);
	}
	
	@OperateLog(module = "迁移单管理",desc="编辑迁移单", type = OpType.UPDATE)
	@RequestMapping(value="edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Transfer transfer){
		transferService.edit(transfer);
		return setSuccessModelMap(modelMap,null);
	}
	
	
	@RequestMapping(value="qryById")
	public ResponseEntity<ModelMap> qryById(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id){
		Transfer data= transferService.qryById(id);
		return setSuccessModelMap(modelMap,data);
	}
	
	/**
	 * 查询迁移单列表
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping(value="queryTransferList")
	public ResponseEntity<ModelMap> queryTransferList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String,Object> params){
		boolean isAdmin = (boolean) request.getSession().getAttribute("isAdmin");
		if(!isAdmin) {
			params.put("operatorBy", WebUtil.getCurrentUser());
		}
		@SuppressWarnings("unchecked")
		Page<Transfer> page = (Page<Transfer>) super.getPage(params);
		Page<Transfer> data = transferService.queryTransferList(page,params);
		return setSuccessModelMap(modelMap,data);
	}
	
	/**
	 * 查询迁移单详情列表
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping(value="queryTransferDetailList")
	public ResponseEntity<ModelMap> queryTransferDetailList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String,Object> params){
		@SuppressWarnings("unchecked")
		Page<TransferDetail> page = (Page<TransferDetail>) super.getPage(params);
		Page<TransferDetail> data = transferService.queryTransferDetailList(page,params);
		return setSuccessModelMap(modelMap,data);
	}
	
	@RequestMapping(value="queryDetailById")
	public ResponseEntity<ModelMap> queryTransferDetailList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id){
		TransferDetail data = transferService.queryDetailById(id);
		return setSuccessModelMap(modelMap,data);
	}
	/**
	 * 确认迁移
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@OperateLog(module = "迁移单管理",desc="确认迁移", type = OpType.UPDATE)
	@RequestMapping(value="doTransfer")
	public ResponseEntity<ModelMap> doTransfer(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Transfer transfer){
		transferService.doTransfer(transfer);
		return setSuccessModelMap(modelMap,null);
	}
	
	/**
	 * 所在机房,设备类型,品牌,设备名称模糊查找资产
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "querAssetListByParam")
	public ResponseEntity<ModelMap> queryDetailList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String, Object> params) {	
		@SuppressWarnings("unchecked")
		Page<ItAssetVo> page = (Page<ItAssetVo>) super.getPage(params);
		Page<ItAssetVo> data = transferService.querAssetListByParam(page,params);
		return setSuccessModelMap(modelMap, data);
	}
}
